//prepare long-form census sample for matching to PIKs

//loop over states
local states `"ak al ar az ca co ct de fl ga hi ia id il in ks ky la ma md me mi mn mo ms mt nc nd ne nh nj nm nv ny oh ok or pa ri sc sd tn tx ut va vt wa wi wv wy"'
local i = 0
qui{
foreach st in `states'{
	local i `++i'
	noi di as result "Working on state `i': `st'"
	
	//step 1: read in geography file. keep only a few things
	use "$data/Census/2000/stata/census_hdf_`st'_01", clear
	keep bcseq state county tract block blkgrp ur //basic geography
	tempfile block
	save `block'

	//step 2: read in housing file. keep only a few things
	use "$data/Census/2000/stata/census_hdf_`st'_02", clear
	keep hseq stenure mafid rrt
	tempfile house
	save `house'

	//get person-level information
	use "$data/Census/2000/stata/census_hdf_`st'_03", clear
	keep bcseq hseq pnc qrel qsex qdb qspan imprace pseq qage

	//create birth year variable
	gen birth_yr = substr(qdb, 1, 4)
	destring birth_yr, replace

	merge m:1 bcseq using `block', keep(match) nogen //merge on geography
	merge m:1 hseq using `house', keep(match) nogen //merge on housing stuff
	gen temp = (birth_yr>=1985 & birth_yr<=1994) //relaxed range to be safe

	//only keep if appropriately-aged kid is in the household
	bys mafid: egen kid = max(temp)
	keep if kid
	drop temp kid //nix unecessary variables
	compress
	save "$temp/census_short_`st'", replace //save
}
}


use "$data/Census/2000/stata/census_hdf_al_03", clear


exit //end of dofile



